package com.jkt.rms.businessservice.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.jkt.rms.business.ResourceRole;
import com.jkt.rms.business.ResourceRoleHistory;
import com.jkt.rms.businessservice.ResourceRoleBusinessService;
import com.jkt.rms.commons.utils.EqualityUtils;
import com.jkt.rms.commons.utils.RMSException;
import com.jkt.rms.constants.IConstants;
import com.jkt.rms.dataservice.ResourceRoleDataService;
import com.jkt.rms.dataservice.ResourceRoleHistoryDataService;
import com.jkt.rms.vo.ResourceRoleVO;

public class ResourceRoleBusinessServiceImpl implements
		ResourceRoleBusinessService {

	private static final Logger LOG = Logger
			.getLogger(ResourceRoleBusinessServiceImpl.class);

	private ResourceRoleDataService resourceRoleDataService;

	private ResourceRoleHistoryDataService resourceRoleHistoryDataService;

	/**
	 * set the Resource History Data Service.
	 * @param resourceRoleHistoryDataService.
	 */
	 public void setResourceRoleHistoryDataService(ResourceRoleHistoryDataService
			 resourceRoleHistoryDataService) {
	 this.resourceRoleHistoryDataService = resourceRoleHistoryDataService;
	 }

	/**
	 * set the Resource Role Data service.
	 */
	public void setResourceRoleDataService(
			ResourceRoleDataService resourceRoleDataService) {
		this.resourceRoleDataService = resourceRoleDataService;
	}

	/**
	 * save the Role
	 * 
	 * @param resourceRoleVO
	 * @return
	 * @throws RMSException
	 */
	public boolean saveRole(ResourceRoleVO resourceRoleVO) throws RMSException {
		boolean isExist = true;
		try {
			isExist = resourceRoleDataService.isRoleExist(resourceRoleVO
					.getRoleCode(), resourceRoleVO.getRoleName(), null);
			if (!isExist) {
				resourceRoleVO.setUpdatedBy("User Name");
				resourceRoleVO.setStatus('Y');
				resourceRoleDataService
						.saveRole(getResourceRole(resourceRoleVO));
			}
		} catch (RMSException e) {
			LOG.error("Error Occurs in save Project Role :", e.getCause());
			throw new RMSException(e.getBusinessCode(),
					"Error Occurs while saving the Project Role");
		} catch (Exception ex) {
			LOG.error("Error Occurs in save Project Role :", ex.getCause());
			throw new RMSException("error.application",
					"Error Occurs while saving the Project Role");
		}
		return isExist;
	}

	/**
	 * update the Role
	 */
	public boolean updateRole(ResourceRoleVO resourceRoleVO)
			throws RMSException {
		Long id = Long.valueOf(resourceRoleVO.getId());
		boolean isExist = resourceRoleDataService.isRoleExist(resourceRoleVO
				.getRoleCode(), resourceRoleVO.getRoleName(), id);
		if (!isExist) {
			try {
				resourceRoleVO.setUpdatedBy("User Name");
				ResourceRole ResourceRole = resourceRoleDataService
						.findById(id);
				resourceRoleHistoryDataService.saveRole(getResourceRoleHistroy(ResourceRole));
				resourceRoleDataService
						.updateRole(getResourceRole(resourceRoleVO));
			} catch (RMSException e) {
				LOG
						.error("Error Occurs in saving Project Role :", e
								.getCause());
				throw new RMSException(e.getBusinessCode(),
						"Error Occurs while saving the Project Role");
			} catch (Exception ex) {
				LOG.error("Error Occurs in save Vendor :", ex.getCause());
				throw new RMSException("error.application",
						"Error Occurs while saving the Project Role");
			}
		}
		return isExist;
	}

	/**
	 * find all roles
	 */
	public List<ResourceRoleVO> findAllRoles() throws RMSException {
		final List<ResourceRole> projRoleList = resourceRoleDataService
				.findAll();
		return getResourceRolesVOList(projRoleList);
	}

	/**
	 * Find roles based on search criteria
	 */
	public List<ResourceRoleVO> findByCriteria(ResourceRoleVO projRoleVO)
			throws RMSException {
		List<ResourceRole> projRoleList = null;
		if ((projRoleVO.getRoleName() != null && (!projRoleVO.getRoleName()
				.equals(IConstants.EMPTY_STRING)))
				|| (projRoleVO.getRoleCode() != null && (!projRoleVO
						.getRoleCode().equals(IConstants.EMPTY_STRING)))) {
			try {
				projRoleList = resourceRoleDataService.findByCriteria(getResourceRole(projRoleVO));
			} catch (RMSException e) {
				LOG.error("Exception occured in search Project Role", e
						.getCause());
				throw new RMSException(e.getBusinessCode());
			}
		}
		return getResourceRolesVOList(projRoleList);
	}

	/**
	 * this method return the ResourceRole History BO
	 * @param resourceRole
	 * @return
	 * @throws RMSException
	 */
	 private ResourceRoleHistory getResourceRoleHistroy(ResourceRole resourceRole)throws RMSException
	 {
	 final ResourceRoleHistory resourceRoleHistory = new ResourceRoleHistory();
	 EqualityUtils.copyProperties(resourceRoleHistory,resourceRole);
	 return resourceRoleHistory;
	 }
	/**
	 * This method convert the ResourceRole VO to ResourceRole BO. return
	 * project Role Business object
	 */
	private ResourceRole getResourceRole(ResourceRoleVO resourceRoleVO)
			throws RMSException {
		final ResourceRole ResourceRole = new ResourceRole();
		try {
			EqualityUtils.copyProperties(ResourceRole, resourceRoleVO);
			ResourceRole.setUpdatedDate(new Timestamp(System
					.currentTimeMillis()));
		} catch (Exception e) {
			LOG.error("Error occur in getResourceRole method:" + e.getCause());
			throw new RMSException("error.application");
		}
		return ResourceRole;
	}

	/**
	 * This method convert the ResourceRole BO to ResourceRole VO. return
	 * project Role Business object
	 */
	private ResourceRoleVO getResourceRoleVO(ResourceRole ResourceRole)
			throws RMSException {
		final ResourceRoleVO resourceRoleVO = new ResourceRoleVO();
		try {
			EqualityUtils.copyProperties(resourceRoleVO, ResourceRole);
		} catch (Exception e) {
			LOG
					.error("Error occur in getResourceRoleVO method:"
							+ e.getCause());
			throw new RMSException("error.application");
		}
		return resourceRoleVO;
	}

	/**
	 * This method convert the list of ResourceRole BO to list of ResourceRole
	 * VO.s return project Role Business object
	 */
	private List<ResourceRoleVO> getResourceRolesVOList(
			List<ResourceRole> projRoleList) throws RMSException {
		ResourceRoleVO resourceRoleVO=null;
		List<ResourceRoleVO> projRoleVOList = null;
		if (projRoleList != null) {
			projRoleVOList = new ArrayList<ResourceRoleVO>();
			for (ResourceRole projRole : projRoleList) {
				try {
					resourceRoleVO=getResourceRoleVO(projRole);
					resourceRoleVO=changeStatus(resourceRoleVO);
					projRoleVOList.add(resourceRoleVO);
					} catch (RMSException e) {
					LOG.error(" Exception occured in getResourceRolesVOList:"
							+ e.getCause());
					throw new RMSException("error.application");
				}
			}
		}
		return projRoleVOList;
	}
	
	/**
	 * 
	 * @param resourceRoleVO
	 * @return
	 */
	
	private ResourceRoleVO changeStatus(ResourceRoleVO resourceRoleVO) {
		char status=resourceRoleVO.getStatus();
		switch(status){
		case 'Y':{
			resourceRoleVO.setShowStatus(IConstants.ACTIVE_STATUS);
			break;
		}
		case 'N':{
			resourceRoleVO.setShowStatus(IConstants.INACTIVE_STATUS);
			break;
		}
		}
		return resourceRoleVO;
	}



	}
